/**
 * @Author: 陈瑞鹏
 * @Date:   2022-11-30 09:31:32
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2022-11-30 09:31:37
 */
/**
 * 	字典
 *      let dict = new Dictionary<string,string>();
 *		dict.set('mytest','HAHA');
 *		dict.set('utest','BOBO');
 *		dict.delete('mytest'); //return item's [mytest] value : 'HAHA'
 *		dict.getWithComponent('mytest');//null
 *		dict.getWithComponent('');//null
 *		dict.getWithComponent('utest');//"BOBO"
 */
export class Dictionary<TypeKeyT, TypeValueT>
{
  private _typeKeyList: TypeKeyT[] = new Array<TypeKeyT>();
  private _typeValueList: TypeValueT[] = new Array<TypeValueT>();

  public set(key: TypeKeyT, value: TypeValueT): void {
    let index = this._typeKeyList.indexOf(key);
    if (index == -1) {
      this._typeKeyList.push(key);
      index = this._typeKeyList.indexOf(key);
    }
    this._typeValueList[index] = value;
  }

  public get(key: TypeKeyT): TypeValueT {
    let index = this._typeKeyList.indexOf(key);
    if (index != -1) {
      return this._typeValueList[index];
    }
    return null;
  }

  public delete(key: TypeKeyT): TypeValueT {
    let index = this._typeKeyList.indexOf(key);
    if (index != -1) {
      this._typeKeyList.splice(index, 1);
      let deleteList = this._typeValueList.splice(index, 1);
      return deleteList[0];
    }
    return null;
  }

  public clear(): void {
    for (let k = this._typeKeyList.length - 1; k >= 0; --k) {
      this.delete(this._typeKeyList[k]);
    }
  }

  public containsKey(key: TypeKeyT): boolean {
    if (this._typeKeyList.indexOf(key) >= 0) {
      return true;
    }
    return false;
  }

  public getKeyList(): any[] {
    return this._typeKeyList;
  }

  public getValueList(): any[] {

    return this._typeValueList;
  }

  public getKeyValueList(): any {

    let keyList = this.getKeyList();

    let arr = {}

    for (let i: number = 0; i < keyList.length; i++) {

      arr[keyList[i]] = this.get(keyList[i]);
    }

    return arr;
  }

  public length(): number {
    return this._typeKeyList.length;
  }
}